package com.mall.mall.coupon.service.impl;

import com.mall.common.To.MemberPrice;
import com.mall.common.To.SkuReductionTo;
import com.mall.mall.coupon.entity.MemberPriceEntity;
import com.mall.mall.coupon.entity.SkuLadderEntity;
import com.mall.mall.coupon.service.MemberPriceService;
import com.mall.mall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;

import com.mall.mall.coupon.dao.SkuFullReductionDao;
import com.mall.mall.coupon.entity.SkuFullReductionEntity;
import com.mall.mall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    SkuLadderService ladderService;

    @Autowired
    MemberPriceService priceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReductionInfo(SkuReductionTo to) {
        //1.保存打折信息
        if(to.getFullCount() > 0){
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(to.getSkuId());
            skuLadderEntity.setFullCount(to.getFullCount());
            skuLadderEntity.setDiscount(to.getDiscount());
            skuLadderEntity.setAddOther(to.getCountStatus());
            ladderService.save(skuLadderEntity);
        }

        //2.保存满减信息
        if (to.getFullPrice().compareTo(BigDecimal.ZERO) == 1){
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(to, skuFullReductionEntity);
            this.save(skuFullReductionEntity);
        }

        //3.保存会员价
        List<MemberPrice> memberPrice = to.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(to.getSkuId());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setAddOther(1);

            return memberPriceEntity;
        }).filter(item->{
            return item.getMemberPrice().compareTo(BigDecimal.ZERO) == 1;
        }).collect(Collectors.toList());

        priceService.saveBatch(collect);

    }

}